package LearnAlgorithm.e_多维数组and矩阵;

import util.Util;

public class b二维数组去掉含有零的行和列 {
	public static void main(String[] args) {
		int[][] matrix = new int[][] {
			{1,2,3,4},
			{5,0,7,8},
			{9,10,11,12},
			{13,14,15,16},
		};
		solve(matrix);
		Util.printMatrix(matrix);
	}
	
	public static void solve(int[][] matrix) {
		int TotalRow = matrix.length;
		int TotalCol = matrix[0].length;
		int[] rowRecord = new int[TotalCol];//记录出现0的行row
		int[] colRecord = new int[TotalCol];//记录出现0的列col
		for (int row = 0; row < TotalRow; row++) {
			for (int col = 0; col < TotalCol; col++) {
				if (matrix[row][col] == 0) {//遍历matrix全部元素;发现某个元素数值=0
					rowRecord[row] = 1;//该元素的row索引值被记录到对应的rowRecord[]的索引
					colRecord[col] = 1;//该元素的col索引值被记录到对应的colRecord[]的索引
				}
			}
		}
		for (int row = 0; row < TotalRow; row++) {//遍历matrix全部元素;
			for (int col = 0; col < TotalCol; col++) {//如果当前遍历的行row或列col
				if (rowRecord[row] == 1 || colRecord[col] == 1) {//被记录到了rowRecord[]或colRecord[]中
					matrix[row][col] = 0;//那么当前遍历的这一个元素的值赋值成0
				}
			}
		}
	}
}
